package com.zhiyizhen.handheld.component.message.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.ResourcesUtil;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.message.bean.MessageItemBean;
import com.zhiyizhen.handheld.databinding.ItemMessageBinding;

/**
 * 我的消息
 */
public class MessageAdapter extends BaseAdapter<MessageItemBean, MessageAdapter.MessageViewHolder> {
    private Context context;

    private OnItemReadListener onItemReadListener;

    public MessageAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setOnItemReadListener(OnItemReadListener onItemReadListener) {
        this.onItemReadListener = onItemReadListener;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MessageViewHolder(this, ItemMessageBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class MessageViewHolder extends BaseViewHolder<MessageItemBean> {

        private final ItemMessageBinding binding;

        public MessageViewHolder(BaseAdapter<MessageItemBean, MessageViewHolder> adapter, ItemMessageBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(MessageItemBean data, int position) {
            if (TextUtils.equals(data.getIsRead(), "1")) {
                binding.timeTv.setTextColor(ResourcesUtil.getColor(R.color.black_48));
                binding.titleTv.setTextColor(ResourcesUtil.getColor(R.color.black_48));
                binding.contentTv.setTextColor(ResourcesUtil.getColor(R.color.black_48));
//                binding.checkbox.setEnabled(false);
            } else {
                binding.timeTv.setTextColor(ResourcesUtil.getColor(R.color.black_e6));
                binding.titleTv.setTextColor(ResourcesUtil.getColor(R.color.black_e6));
                binding.contentTv.setTextColor(ResourcesUtil.getColor(R.color.black_e6));
//                binding.checkbox.setEnabled(true);
            }
            binding.checkbox.setChecked(data.isChecked());

            binding.timeTv.setText(data.getNoticeTime());
            binding.contentTv.setText(data.getNoticeContent());
            binding.titleTv.setText(data.getNoticeName());

            binding.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> data.setChecked(isChecked));


            if (data.isExpand()) {
                binding.contentTv.setVisibility(View.VISIBLE);
                binding.arrowImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.icon_up));
            } else {
                binding.contentTv.setVisibility(View.GONE);
                binding.arrowImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.icon_down));
            }
            binding.arrowImage.setOnClickListener(v -> {
                setRead(data);
            });
            binding.contentLayout.setOnClickListener(v -> {
                setRead(data);
            });

            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        private void setRead(MessageItemBean data) {
            if (binding.contentTv.getVisibility() == View.GONE) {
                binding.contentTv.setVisibility(View.VISIBLE);
                binding.arrowImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.icon_up));
                data.setExpand(true);
            } else {
                binding.contentTv.setVisibility(View.GONE);
                binding.arrowImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.icon_down));
                data.setExpand(false);
            }
            if (!TextUtils.equals(data.getIsRead(), "1")) {
                if (onItemReadListener != null) {
                    onItemReadListener.onItemRead(data);
                }
            }
        }
    }

    public interface OnItemReadListener {
        void onItemRead(MessageItemBean data);
    }

}
